from django.shortcuts import render

# Create your views here.
# {
#     'slide_news': [{第1条新闻}, {第2条新闻}, ...],
#     'top_news': [{第1条新闻}, {第2条新闻}, ...],
#     'image_news': [{第1条新闻}, {第2条新闻}, ...]
#         }
from rest_framework.response import Response
from rest_framework.views import APIView

from news.models import News, NewsCategory
from news.serializers import NewsSlideSerializer, NewsTopSerializer, NewsImageSerializer, \
    OneNewsCategoryAlbumSerializer, TwoNewsCategoryAlbumSerializer


class NewsListView(APIView):
    def get(self, request):
        """
        获取界面顶部的新闻  轮播图新闻、推荐新闻、图片新闻
        1、从数据库中获取新闻信息
        2、将数据进行序列化
        3、返回应答
        """
        # 1、从数据库中获取新闻信息
        slide_new = News.objects.filter(is_slide=1)
        top_new = News.objects.all().order_by('-create_time')[0:10]
        image_new = News.objects.exclude(img_url='').order_by('-click')[0:4]


        # 2、将数据进行序列化
        serializer_slide = NewsSlideSerializer(slide_new, many=True)
        serializer_top = NewsTopSerializer(top_new, many=True)
        serializer_image = NewsImageSerializer(image_new, many=True)

        # 3、返回应答
        response_data = {
            'slide_news': serializer_slide.data,
            'top_news': serializer_top.data,
            'image_news': serializer_image.data,
        }
        return Response(response_data)


# 2.获取类别新闻（包含界面右侧显示的新闻：每个类别下点击量最多的8条新闻）
# URL:  /news/category/
# 请求方式：GET
# 返回的数据格式:json
# [{   # 第1个大类别
#     'id': 3,
#     'title': '互联网媒体',
#     'newscategory_set': [{'id':x1, 'title': '子类别1'},
#                          {'id':x2, 'title': '子类别2'}],
#     'news': [],
#     'top8': [],
#             },
#     ......
# ]

class NewsCategoryView(APIView):
    def get(self, request):
        # 获取类别新闻
        # 获取所有的一级分类NewsCategory.objects.filter(parent_id=0)
        one_news = NewsCategory.objects.filter(parent_id__exact=0)
        # 存储所有的大类数据
        newsData_list_data = []

        for oneNew in one_news:
            # 序列化第一分类的数据
            one_new = OneNewsCategoryAlbumSerializer(oneNew).data
            # 获取当前第一分类下的所有二级分类
            two_news = oneNew.newscategory_set.all()

            # 用于存储二级分类的列表
            newscategory_set = []
            twoNew_id_list = []

            for twoNew in two_news:
                two_New = OneNewsCategoryAlbumSerializer(twoNew).data
                twoNew_id_list.append(two_New['id'])
                newscategory_set.append(two_New)

            one_new['newscategory_set'] = newscategory_set

            # 存储当前类别下时间最新的4条
            news_to4 = News.objects.filter(category_id__in=twoNew_id_list).order_by('-create_time').exclude(img_url='')[
                       0:4]
            news = TwoNewsCategoryAlbumSerializer(news_to4, many=True).data
            one_new['news'] = news

            # 存储当前类别下点击量最高的8条
            news_to8 = News.objects.filter(category_id__in=twoNew_id_list).order_by('-click')[0:8]
            to8 = TwoNewsCategoryAlbumSerializer(news_to8, many=True).data
            one_new['top8'] = to8
            newsData_list_data.append(one_new)

        return Response(newsData_list_data)











